package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;
import java.io.*;

public class LoginServlet implements Serializable{
    public void service(HttpRequest request, HttpResponse response){
        System.out.println("开始登录...");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+","+password);

        if (username==null || password==null){
            File file = new File("./webapps/myweb/login_oo.html");
            response.setEntity(file);
            return;
        }
        File file = new File(username+".user");
        if (file.exists()){
            try (
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis)
            ){
                User u = (User) ois.readObject();
                if (u.getPassword().equals(password)){
                    file = new File("./webapps/myweb/login_success.html");
                    response.setEntity(file);
                    return;
                }else {
                    file = new File("./webapps/myweb/login_xx.html");
                    response.setEntity(file);
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }else {
            file = new File("./webapps/myweb/login_fail.html");
            response.setEntity(file);
        }
        System.out.println("登录处理完成.");
    }
}